package com.webex.ta.hydra;

import bsh.EvalError;
import bsh.Interpreter;
import com.webex.ta.hydra.core.KeywordRegistry;
import com.webex.ta.hydra.core.PropertyManager;
import com.webex.ta.hydra.gui.GuiPackage;
import com.webex.ta.hydra.gui.HydraIdeFrame;
import com.webex.ta.hydra.gui.SplashScreen;
import com.webex.ta.hydra.gui.action.ActionRouter;
import com.webex.ta.hydra.gui.util.ComponentUtil;
import com.webex.ta.hydra.plugin.PluginManager;
import com.webex.ta.hydra.util.HydraUtils;
import com.incors.plaf.alloy.AlloyLookAndFeel;

import javax.swing.*;
import java.awt.*;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-8-30
 * Time: 16:31:45
 */
public class Hydra {
    static {
        AlloyLookAndFeel.setProperty("alloy.licenseCode", "v#ej_technologies#uwbjzx#e6pck8");
        AlloyLookAndFeel.setProperty("alloy.menuLayoutStyle", "noAdjustment");

//        AlloyLookAndFeel.setProperty("alloy.isLookAndFeelFrameDecoration",
//                            "true");
//            AlloyTheme theme = new GlassTheme();
        LookAndFeel alloyLnF = new AlloyLookAndFeel();
        alloyLnF.getDefaults().put("iconTextGap", new Integer(1));
//            alloyLnF.getDefaults().put("Menu.foreground", Color.RED);

        try {
            UIManager.setLookAndFeel(alloyLnF);
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }

        Font font = new Font("Dialog", Font.PLAIN, 11);
//        Font menuFont = font.deriveFont(font.getStyle(), 11);

        UIManager.put("Label.font", font);
        UIManager.put("List.font", font);
        UIManager.put("CheckBox.font", font);
        UIManager.put("ComboBox.font", font);
        UIManager.put("EditorPane.font", font);

        UIManager.put("Menu.font", font);
        UIManager.put("MenuItem.font", font);
        UIManager.put("RadioButton.font", font);
        UIManager.put("Table.font", font);
        UIManager.put("TableHeader.font", font);
        UIManager.put("TabbedPane.font", font);
        UIManager.put("TextField.font", font);
        UIManager.put("TextArea.font", font);
        UIManager.put("TextPane.font", font);
        UIManager.put("EditPane.font", font);
        UIManager.put("TitleBorder.font", font);
    }


    public void start(String[] args) throws EvalError {
        HydraUtils.setHydraHome(HydraStartup.getHydraDir());
        JFrame frame = new HydraIdeFrame();
        frame.setSize(800, 600);
        ComponentUtil.centerComponentInWindow(frame);


        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);


        SplashScreen splash = new SplashScreen();
        splash.setSize(splash.getPreferredSize());
        ComponentUtil.centerComponentInWindow(splash);
        splash.setVisible(true);
        try {
            splash.setLabel("Initializing property manager");
            Thread.sleep(300);
            PropertyManager.getInstance();
            splash.setLabel("Initializing action router");
            Thread.sleep(300);
            ActionRouter.getInstance();


//        Interpreter bsh = new Interpreter();
//        bsh.setClassLoader(HydraStartup.getClassLoader());
//        Object obj = bsh.eval("new com.webex.ta.hydra.hamal.action.HamalExecute()");
//        System.out.println(obj.getClass());

            splash.setLabel("Initializing plugins");
            Thread.sleep(300);
            PluginManager.getInstance().initInstalledPlugins();
            splash.setLabel("Initializing keywords");
            Thread.sleep(300);
            KeywordRegistry.getInstance();
        } catch (InterruptedException ignore) {
        }

        splash.setVisible(false);
        frame.setVisible(true);

        frame.requestFocus();
        GuiPackage.getInstance().setTestPlanFile(null);
//        ComponentUtil.centerComponentInWindow(frame);

    }
}
